/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Employee.java
 *
 * Created on May 6, 2011, 7:19:52 PM
 */
package com.abc.salesinventory.ui;

import com.abc.salesinventory.model.*;
import com.abc.salesinventory.service.ItemService;
import com.abc.salesinventory.service.ItemServiceImpl;
import com.abc.salesinventory.util.CategoryXMLReader;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author Sasika Suchithra
 */
public class NewMedicine extends javax.swing.JFrame {

    private ItemService itemService = new ItemServiceImpl();
    List<Category> categories;

    /** Creates new form Employee */
    public NewMedicine() {
        initComponents();
        loadCategories();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        brandName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        supplier = new javax.swing.JTextField();
        btnsave = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        btncls = new javax.swing.JButton();
        cmbUnit = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        unitPrice = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        reoderLevel = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cmbCategory = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        itemcode = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Medicine");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);
        setLocationByPlatform(true);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Category");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 130, -1, -1));

        brandName.setBackground(new java.awt.Color(219, 237, 255));
        jPanel1.add(brandName, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 90, 190, -1));

        jLabel3.setText("Supplier");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 170, -1, -1));

        supplier.setBackground(new java.awt.Color(219, 237, 255));
        supplier.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                supplierKeyPressed(evt);
            }
        });
        jPanel1.add(supplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 170, 190, -1));

        btnsave.setText("Save");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });
        jPanel1.add(btnsave, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 300, 70, -1));

        jLabel5.setText("Enter new Medicine details.");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        btncls.setText("Close");
        btncls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnclsActionPerformed(evt);
            }
        });
        jPanel1.add(btncls, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 300, 70, -1));

        cmbUnit.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Unit--", "g", "ml", "tablet", "packet" }));
        cmbUnit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbUnitActionPerformed(evt);
            }
        });
        jPanel1.add(cmbUnit, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 250, 90, -1));

        jLabel6.setText("Brand Name");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 90, -1, -1));

        unitPrice.setBackground(new java.awt.Color(219, 237, 255));
        unitPrice.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                unitPriceKeyPressed(evt);
            }
        });
        jPanel1.add(unitPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 210, 190, -1));

        jLabel7.setText("Unit Price (Rs.)");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 210, -1, -1));

        reoderLevel.setBackground(new java.awt.Color(219, 237, 255));
        reoderLevel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                reoderLevelKeyPressed(evt);
            }
        });
        jPanel1.add(reoderLevel, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 250, 90, -1));

        jLabel8.setText("Re-order Level");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 250, -1, -1));

        cmbCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Category--", "Tablet", "Capsule", "Syrup", "Liquid", "Powder", "Other" }));
        cmbCategory.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCategoryItemStateChanged(evt);
            }
        });
        cmbCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbCategoryActionPerformed(evt);
            }
        });
        jPanel1.add(cmbCategory, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 130, 190, -1));

        jLabel9.setText("Code");
        jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, -1, -1));

        itemcode.setBackground(new java.awt.Color(219, 237, 255));
        jPanel1.add(itemcode, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 190, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnclsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnclsActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnclsActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        if (itemcode.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Code !", "Save Medicine", 2);
        } else if (brandName.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Brand Name !", "Save Medicine", 2);
        } else if (cmbCategory.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(null, "Select Category !", "Save Medicine", 2);
        } else if (supplier.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Supplier !", "Save Medicine", 2);
        } else if (unitPrice.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Unit Price !", "Save Medicine", 2);
        } else if (reoderLevel.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Enter Re-order Level !", "Save Medicine", 2);
        } else if (cmbUnit.getSelectedItem().toString().contains("Unit")) {
            JOptionPane.showMessageDialog(null, "Select units !", "Save Medicine", 2);
        } else {
            Item item = new Item();
            item.setItemCode(itemcode.getText());
            item.setBrandName(brandName.getText());
            item.setCategory(cmbCategory.getSelectedItem().toString());
            item.setSupplier(supplier.getText());
            item.setUnitPrice(Double.parseDouble(unitPrice.getText()));
            item.setReorderLevel(Integer.parseInt(reoderLevel.getText()));
            item.setUnit(cmbUnit.getSelectedItem().toString());

            boolean save = itemService.save(item);

            if (save) {
                itemcode.setText(null);
                brandName.setText(null);
                cmbCategory.setSelectedIndex(0);
                cmbUnit.setSelectedIndex(0);
                supplier.setText(null);
                unitPrice.setText(null);
                reoderLevel.setText(null);
            }
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void supplierKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_supplierKeyPressed
    }//GEN-LAST:event_supplierKeyPressed

    private void unitPriceKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_unitPriceKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_unitPriceKeyPressed

    private void cmbUnitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbUnitActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbUnitActionPerformed

    private void reoderLevelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_reoderLevelKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_reoderLevelKeyPressed

    private void cmbCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbCategoryActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbCategoryActionPerformed

    private void cmbCategoryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCategoryItemStateChanged
        loadUnits();
    }//GEN-LAST:event_cmbCategoryItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewMedicine().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField brandName;
    private javax.swing.JButton btncls;
    private javax.swing.JButton btnsave;
    private javax.swing.JComboBox cmbCategory;
    private javax.swing.JComboBox cmbUnit;
    private javax.swing.JTextField itemcode;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField reoderLevel;
    private javax.swing.JTextField supplier;
    private javax.swing.JTextField unitPrice;
    // End of variables declaration//GEN-END:variables

    private void loadCategories() {
        categories = CategoryXMLReader.readCategories();
        Vector vec = new Vector();
        vec.add("---Select Category---");
        for (Iterator<Category> it = categories.iterator(); it.hasNext();) {
            Category category = it.next();
            vec.add(category.getName());
        }
        cmbCategory.setModel(new DefaultComboBoxModel(vec));
    }

    private void loadUnits() {
        int selectedIndex = cmbCategory.getSelectedIndex();

        if (selectedIndex > 0) {
            Vector vec = new Vector();
            Category selectedCategory = categories.get(selectedIndex - 1);
            List<String> units = selectedCategory.getUnits();
            if (units.size() > 1) {
                vec.add("---Unit---");
            }
            for (int i = 0; i < units.size(); i++) {
                vec.add(units.get(i));
            }
            cmbUnit.setModel(new DefaultComboBoxModel(vec));
        }
    }
}
